package com.goodving.guli.model.weightsyncupload;

import android.content.Context;
import android.text.TextUtils;

import com.goodving.guli.bt.model.LoginData;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.model.uploadhelper.SyncerUpload;
import com.goodving.guli.model.uploadhelper.p074a.C1300g;
import com.goodving.guli.model.uploadhelper.p074a.C1304h;
import com.goodving.guli.model.uploadhelper.p074a.C1306a;
import com.goodving.guli.model.uploadhelper.p074a.C1309e;
import com.goodving.guli.model.uploadhelper.p074a.C1311i;
import com.goodving.guli.model.uploadhelper.p074a.p075a.C1301a;
import com.goodving.guli.model.uploadhelper.p074a.p075a.ResponseHandler;
import com.goodving.guli.model.uploadhelper.p076b.C1313a;
import com.goodving.guli.server.ClientDataConstants;
import com.goodving.guli.server.ClientConstant;
import com.goodving.guli.server.URL;
import com.goodving.guli.view.C3657a;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/* renamed from: cn.com.smartdevices.bracelet.weight.a.l */
public class SyncerWeightUpload extends SyncerUpload<Integer> { // C2004l
    private static final String f11292a = "Syncer.Weight.Upload";
    private Context f11293b;
    private LoginData f11294c;

    public SyncerWeightUpload(Context context, LoginData loginData, Integer num) {
        super(num);
        this.f11293b = context;
        this.f11294c = loginData;
    }

    public class C2007o extends C1309e<List<WeightInfo>> {
        public void m11511a(List<WeightInfo> list) {
            for (WeightInfo weightInfo : list) {
                weightInfo.synced = 1;
            }
            success(WeightInfoManager.getInstance().updateInfos((List) list));
        }
    }

    public class C2009q extends C1311i<List<WeightInfo>, String> {
        public String m15686a(List<WeightInfo> list) {
            String str = C3657a.f18328f;
            try {
                JSONArray jSONArray = new JSONArray();
                for (WeightInfo weightInfo : list) {
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("fuid", weightInfo.uid);
                    jSONObject.put("wt", Float.valueOf(weightInfo.weight));
                    jSONObject.put("ts", weightInfo.timestamp / 1000);
                    if (!TextUtils.isEmpty(weightInfo.deviceid)) {
                        jSONObject.put("did", weightInfo.deviceid);
                    }
                    jSONObject.put("wdt", weightInfo.type);
                    jSONArray.put(jSONObject);
                }
                return jSONArray.toString();
            } catch (JSONException e) {
                e.printStackTrace();
                return str;
            }
        }

        public /* synthetic */ String wrap(List<WeightInfo> obj) {
            return m15686a((List) obj);
        }
    }

    class C2005m extends C1311i<String, RequestParams> {
        final /* synthetic */ SyncerWeightUpload f11295a;

        C2005m(SyncerWeightUpload c2004l) {
            this.f11295a = c2004l;
        }

        public RequestParams m15679a(String str) {
            RequestParams a = ClientDataConstants.m11558a(this.f11295a.f11294c);
            a.put(ClientDataConstants.JSONDATA, str);
            a.put(ClientDataConstants.aa, 1);
            return a;
        }

        public /* synthetic */ RequestParams wrap(String obj) {
            return m15679a((String) obj);
        }
    }

    public String tag() {
        return "Syncer.Weight.Upload_@" + Integer.toHexString(hashCode());
    }

    public class C2006n extends C1306a<Integer, List<WeightInfo>> {
        public List<WeightInfo> m15680a(Integer num) {
            return WeightInfoManager.getInstance().m15773d(num.intValue());
        }

        public boolean m15681a(List<WeightInfo> list) {
            return list == null || list.size() == 0;
        }

        public /* synthetic */ List<WeightInfo> access(Integer obj) {
            return m15680a((Integer) obj);
        }

        public /* synthetic */ boolean isEmpty(List<WeightInfo> obj) {
            return m15681a((List) obj);
        }
    }

    public void work() {
        GuliLog.tracePrintInfo(tag(), "Sync Start!!");
        if (this.f11294c.isValid()) {
            try {
                C1313a c1313a = new C1313a(this.mWorkParams);
                C1306a c2006n = new C2006n();
                C1311i c2009q = new C2009q();
                C1300g c1301a = new C1301a(URL.m10531a(ClientConstant.f6781Z));
                C1304h c1305d = new ResponseHandler(this.f11293b);
                C1309e c2007o = new C2007o();
                c1313a = c1313a.m11515a( this, c2006n);
                c1313a.m11520a( this, c2009q).m11520a( this, new C2005m(this)).m11518a( this, c1301a).m11519a( this, c1305d);
                if (c1305d.isSuccessed()) {
                    c1313a.m11517a( this, c2007o);
                    success(c2007o.isSuccessed());
                }
            } catch (Throwable e) {
                GuliLog.m12353a(tag(), "Sync Exception!!", e);
            }
            GuliLog.tracePrintInfo(tag(), "Sync Finish!!");
            return;
        }
        GuliLog.tracePrintInfo(tag(), "Sync Stop!!");
    }
}
